# -*- coding: utf-8 -*-
"""
实现混淆矩阵精准率和召回率
Created on Tue Apr 24 08:39:31 2018

@author: Allen
"""
import numpy as np
from sklearn import datasets

digits = datasets.load_digits()
X = digits.data
y = digits.target.copy()

y[digits.target == 9] = 1
y[digits.target != 9] = 0

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split( X, y, random_state = 666 )

from sklearn.linear_model import LogisticRegression
log_reg = LogisticRegression()
log_reg.fit( X_train, y_train )
print( log_reg.score( X_test, y_test ) ) # 0.975555555556

y_log_predict = log_reg.predict( X_test )

# TF
def TN( y_true, y_predict ):
    return np.sum( ( y_true == 0 ) & ( y_predict == 0 ) )
print( TN( y_test, y_log_predict ) ) # 403

# FP
def FP( y_true, y_predict ):
    return np.sum( ( y_true == 0 ) & ( y_predict == 1 ) )
print( FP( y_test, y_log_predict ) ) # 2

# FN
def FN( y_true, y_predict ):
    return np.sum( ( y_true == 1 ) & ( y_predict == 0 ) )
print( FN( y_test, y_log_predict ) ) # 9

# TP
def TP( y_true, y_predict ):
    return np.sum( ( y_true == 1 ) & ( y_predict == 1 ) )
print( TP( y_test, y_log_predict ) ) # 36

# 混淆矩阵
def comfusion_matrix( y_true, y_predict ):
    return np.array([
                [TN( y_true, y_predict ),FP( y_true, y_predict )],
                [FN( y_true, y_predict ),TP( y_true, y_predict )]
            ])
print( comfusion_matrix( y_test, y_log_predict ) )
'''
[[403   2]
 [  9  36]]
'''

# 精准率
def precision_score( y_true, y_predict ):
    tp = TP( y_true, y_predict )
    fp = FP( y_true, y_predict )
    try:
        return tp / ( tp + fp )
    except:
        return 0.0
    
print( precision_score( y_test, y_log_predict ) ) # 0.947368421053

# 召回率
def recall_score( y_true, y_predict ):
    tp = TP( y_true, y_predict )
    fn = FN( y_true, y_predict )
    try:
        return tp / ( tp + fn )
    except:
        return 0.0
print( recall_score( y_test, y_log_predict ) ) # 0.8

# sklearn 中的混淆矩阵，精准率和召回率
from sklearn.metrics import confusion_matrix
confusion_matrics = confusion_matrix( y_test, y_log_predict )
print( confusion_matrics )
'''
[[403   2]
 [  9  36]]
'''

# 精准率
from sklearn.metrics import precision_score
print( precision_score( y_test, y_log_predict ) ) # 0.947368421053

# 召回率
from sklearn.metrics import recall_score
print( recall_score( y_test, y_log_predict ) ) # 0.8